using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace Banshee.Controls
{
    public class GlassTreeview:TreeView
    {
        
        public GlassTreeview()
        {
            SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
            UpdateStyles();
            this.ShowLines = false;
            this.DrawMode = TreeViewDrawMode.OwnerDrawAll;
            this.HideSelection = false;
        }
        public Rendering.RendererBase Renderer
        {
            get { return ActiveRenderer.Renderer ; }
            
        }
        protected override void OnDrawNode(DrawTreeNodeEventArgs e)
        {
            string exp = ">";
            string col = "<";
            string emp = " ";
            Graphics g = e.Graphics;
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SystemDefault;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            Rectangle rect = e.Bounds;
            Font nodeFont = e.Node.NodeFont;
            if (nodeFont == null) nodeFont = ((TreeView)this).Font;
            if ((e.State & TreeNodeStates.Selected) != 0)
            {


                using (LinearGradientBrush brush = new LinearGradientBrush(rect, Renderer.SelectedGradientStart, Renderer.SelectedGradientEnd, LinearGradientMode.Vertical))
                {
                    g.FillRectangle(brush, rect);
                }
                Color SelectedLightColor = Color.FromArgb(251, 230, 148);
                Color SelectedDarkColor = Color.FromArgb(251, 150, 60);
                Rendering.DrawingMethods.DrawGlassGradient(e.Graphics, rect, SelectedLightColor, SelectedDarkColor, 90F);
            }
            else
            {
                g.FillRectangle(new SolidBrush(BackColor), rect);
            }
            rect.Offset(16, 0);
            if (e.Node.Level != 0)
                rect.Offset(15, 0);
            else
            {
                if (e.Node.Nodes.Count > 0)
                {
                    if (e.Node.IsExpanded)
                        g.DrawString(col, nodeFont, Brushes.Black, e.Bounds);
                    else
                        g.DrawString(exp, nodeFont, Brushes.Black, e.Bounds);
                }
                else
                    g.DrawString(emp, nodeFont, Brushes.Gainsboro, e.Bounds);
            }
            if (this.ImageList != null && e.Node.ImageIndex >= 0 && e.Node.ImageIndex < ImageList.Images.Count)
            {
                this.ImageList.Draw(g, rect.X, rect.Y+2, 16, 16, e.Node.ImageIndex);
            }
            rect.Offset(16, 0);
            e.Graphics.DrawString(e.Node.Text, nodeFont, Brushes.Black,
                 Rectangle.Inflate(rect, 2, 0));

        }

    }
}
